package cn.maihe.elg.operation.service.order;

import cn.maihe.elg.operation.model.enums.ContractStatus;
import cn.maihe.elg.operation.model.enums.ContractType;
import cn.maihe.elg.operation.repository.entity.AcceptOrderContract;
import cn.maihe.elg.operation.repository.entity.AcceptOrderContractCondition;
import cn.maihe.elg.operation.service.BaseDaoService;
import cn.maihe.framework.starter.mybatis.dao.MybatisDaoImpl;
import cn.maihe.framework.starter.mybatis.mapper.PlusEntityWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @Description
 * @Author WJH
 * @Date 2021/08/10
 */
@Service
public class AcceptOrderContractService extends BaseDaoService<AcceptOrderContract> {


    public void save(AcceptOrderContract acceptOrderContract) {
        String acceptOrderNo = acceptOrderContract.getAcceptOrderNo();
        String contractType = acceptOrderContract.getContractType();

        AcceptOrderContract dbContract = MybatisDaoImpl.run().selectOne(AcceptOrderContract.builder().acceptOrderNo(acceptOrderNo).contractType(contractType).build());
        if (dbContract != null) {
            acceptOrderContract.setId(dbContract.getId());
            BeanUtils.copyProperties(acceptOrderContract, dbContract);
            MybatisDaoImpl.run().updateById(dbContract);
        } else {
            MybatisDaoImpl.run().insert(acceptOrderContract);
        }
    }

    public void updateContractStatus(Long anneSignContractId, ContractStatus contractStatus) {
        AcceptOrderContract entity = AcceptOrderContract.builder().contractStatus(contractStatus.name()).build();
        PlusEntityWrapper<AcceptOrderContract> condition = AcceptOrderContractCondition.builder().andChannelContractIdEq(String.valueOf(anneSignContractId)).build();
        this.update(entity, condition);
    }
    public AcceptOrderContract getAcceptOrderContract(String acceptOrderNo, ContractType contractType){
        AcceptOrderContract acceptOrderContract = MybatisDaoImpl.run().selectOne(AcceptOrderContract.builder().acceptOrderNo(acceptOrderNo).contractType(contractType.name()).build());
        return acceptOrderContract;
    }
}
